package com.bond.jumper.common.util;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

public class ImageUtils {

    public static final String FORMAT_JPEG = "jpeg";
    public static final String FORMAT_PNG = "png";

    /**
     * 将BufferedImage编码后返回字节数组
     *
     * @param image
     * @param format
     * @return
     */
    public static byte[] bufferedImageToByteArray(BufferedImage image, String format) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(image, format, baos);
            // 获取编码后的字节数组
            return baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
